package com.haoran.dwz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.haoran.dwz.common.exception.NoExistException;
import com.haoran.dwz.common.exception.ReferenceException;
import com.haoran.dwz.common.exception.RepeatException;
import com.haoran.dwz.common.exception.ValidException;
import com.haoran.dwz.common.handler.PermissionCacheHandler;
import com.haoran.dwz.common.validator.Assert;
import com.haoran.dwz.domain.entities.Permission;
import com.haoran.dwz.domain.entities.SysMenuEntity;
import com.haoran.dwz.domain.pojo.PermissionVO4Add;
import com.haoran.dwz.domain.pojo.PermissionVO4Update;
import com.haoran.dwz.repository.PermissionRepository;
import com.haoran.dwz.service.PermissionService;

@Service
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private PermissionRepository permissionRepository;
	
	
	@Autowired
	private PermissionCacheHandler permissionCacheHandler;

	@Override
	public List<Permission> treeListPermissions() {
		List<Permission> list = new ArrayList<Permission>();
		List<Permission> root = permissionRepository.findRootReources();
		for (Permission resource : root) {
			list.add(resource);
			appendChildren(list, resource);
		}
		return list;
	}

	private void appendChildren(List<Permission> list, Permission resource) {
		List<Permission> children = resource.getChildren();
		if (!children.isEmpty()) {
			for (Permission r : children) {
				list.add(r);
				appendChildren(list, r);
			}
		}
	}

	@Override
	public List<Permission> listAll() {
		return permissionRepository.findAll();
	}

	@Override
	public List<Permission> treePermissions() {
		return permissionRepository.findRootReources();
	}

	@Override
	public Permission save(Permission permission) {
		permission = permissionRepository.save(permission);
		permissionCacheHandler.synchronizePermissionCache();
		return permission;
	}

	@Override
	public Permission newAdd(PermissionVO4Add permissionVO) {
		Assert.isNull(permissionVO, "入参为空");
		checkName(permissionVO.getName());
		checkNumber(permissionVO.getNumber());
		Permission permission = new Permission();
		permission.setPermissionType(permissionVO.getPermissionType());
		permission.setOrderNumber(permissionVO.getOrderNumber());
		permission.setUrl(permissionVO.getUrl());
		permission.setAuth(permissionVO.getAuth());
		permission.setName(permissionVO.getName());
		permission.setDescription(permissionVO.getDescription());
		if(StringUtils.isNotBlank(permissionVO.getParentId())){
			Permission parent = permissionRepository.findOne(permissionVO.getParentId());
			permission.setParent(parent);
		}
		permission.setNumber(permissionVO.getNumber());
		permission = permissionRepository.save(permission);
		permissionCacheHandler.synchronizePermissionCache();
		return permission;
	}
	
	private void checkName(String name) {
		if (StringUtils.isNotBlank(name)) {
			Permission g = permissionRepository.findByName(name);
			if (g != null) {
				throw new RepeatException("已存在的分组名称");
			}
		} else {
			throw new ValidException("分组名称不能为空");
		}
	}

	private void checkNumber(String number) {
		if (StringUtils.isNotBlank(number)) {
			Permission g = permissionRepository.findByNumber(number);
			if (g != null) {
				throw new RepeatException("已存在的分组编码");
			}
		} else {
			throw new ValidException("分组编码不能为空");
		}
	}
	@Override
	public void delete(String id) {
		Assert.isNull(id, "入参为空");
		Permission permission = permissionRepository.findOne(id);
		if (permission.getChildren().isEmpty()) {
			permissionRepository.delete(id);
			permissionCacheHandler.synchronizePermissionCache();
		} else {
			throw new ReferenceException("存在下级权限项，请先删除子权限!");
		}
	}

	@Override
	public void delete(String id, boolean subInclude) {
		Assert.isNull(id, "入参为空");
		Permission permission = permissionRepository.findOne(id);
		List<Permission> children = permission.getChildren();
		if (children.isEmpty()) {
			permissionRepository.delete(id);
			permissionCacheHandler.synchronizePermissionCache();
		} else {
			if (subInclude) {
				permissionRepository.delete(id);
				permissionCacheHandler.synchronizePermissionCache();
			}
			throw new ReferenceException("存在下级权限项，请先删除子权限!");
		}
	}

	@Override
	@Transactional
	public Permission update(PermissionVO4Update permissionVO4Update) {
		Assert.isNull(permissionVO4Update, "入参为空");
		checkName(permissionVO4Update.getName(), permissionVO4Update.getId());
		checkNumber(permissionVO4Update.getNumber(), permissionVO4Update.getId());
		Permission permission = permissionRepository.findOne(permissionVO4Update.getId());
		if (permission == null) {
			throw new NoExistException("不存在的权限项");
		}
		String parentId = permissionVO4Update.getParentId();
		if (StringUtils.isNotBlank(parentId)) {
			Permission parent = permissionRepository.findOne(parentId);
			permission.setParent(parent);
		}
		permission.setPermissionType(permissionVO4Update.getPermissionType());
		permission.setName(permissionVO4Update.getName());
		permission.setNumber(permissionVO4Update.getNumber());
		permission.setDescription(permissionVO4Update.getDescription());
		permission.setUrl(permissionVO4Update.getUrl());
		permission.setAuth(permissionVO4Update.getAuth());
		permission.setOrderNumber(permissionVO4Update.getOrderNumber());
		permission = permissionRepository.save(permission);
		permissionCacheHandler.synchronizePermissionCache();
		return permission;
	}

	private void checkNumber(String number, String id) {
		if (StringUtils.isNotBlank(number)) {
			Permission g = permissionRepository.findByNumber(number, id);
			if (g != null) {
				throw new RepeatException("已存在的权限项名称");
			}
		} else {
			throw new ValidException("权限项不能为空");
		}
	}

	private void checkName(String name, String id) {
		if (StringUtils.isNotBlank(name)) {
			Permission g = permissionRepository.findByName(name, id);
			if (g != null) {
				throw new RepeatException("已存在的权限项名称");
			}
		} else {
			throw new ValidException("权限项不能为空");
		}
	}

	@Override
	public List<SysMenuEntity> getUserMenuList() {
		return null;
	}
}
